package com.zhanvie.signal;

import com.zhanvie.singleton.RunState;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import sun.misc.Signal;
import sun.misc.SignalHandler;

public class KillSignalHandler implements SignalHandler {

    public static final Logger logger = LogManager.getLogger(KillSignalHandler.class);

    public void registerSignal(String signalName) {
        Signal signal = new Signal(signalName);
        Signal.handle(signal, this);
    }

    @Override
    public void handle(Signal signal) {
        switch (signal.getName()) {
            case "INT":
                logger.info("receive signal [INT], program will shutdown soon!");
                RunState.getInstance().stop();
                break;
            case "TERM":
                logger.info("receive signal [TERM], program will shutdown soon!");
                System.exit(0);
            default:
                System.out.println("其他信号：" + signal.getName());
        }

    }

}
